package com.wgchao.amc.controller.base;

import com.wgchao.amc.autho.AuthoInfoCenter;
import com.wgchao.amc.autho.UserInfo;
import com.wgchao.amc.common.BindingError;
import com.wgchao.amc.common.MenuAutho;
import com.wgchao.amc.common.PageData;
import com.wgchao.amc.common.ResultCommon;
import com.wgchao.amc.common.validate.Assign;
import com.wgchao.amc.common.validate.Save;
import com.wgchao.amc.common.validate.Update;
import com.wgchao.amc.mapstruct.dto.SysBaseGroupDTO;
import com.wgchao.amc.mapstruct.dto.SysBaseRoleDTO;
import com.wgchao.amc.mapstruct.dto.SysBaseUserDTO;
import com.wgchao.amc.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName: UserInfoController
 * @author: wgchao
 * @createTime: 2019/10/20 12:26 PM
 * @Description: 用户信息管理
 */
@RestController
@RequestMapping("/amc/sys/base/user")
@Api(tags = "用户信息管理")
public class UserInfoController {
    @Autowired
    private UserService userService;

    @PostMapping("/save")
    @ApiOperation(value = "新增用户")
    @MenuAutho(desc = "新增用户")
    @ApiParam(name = "userDTO", value = "用户信息", required = true)
    public ResultCommon<SysBaseUserDTO> save(@RequestBody @Validated(value = {Save.class}) SysBaseUserDTO userDTO,
                                             BindingResult bindingResult,
                                             HttpSession session) throws Exception{
        String error = null;
        if((error = BindingError.error(bindingResult)) != null){
            return ResultCommon.fail(error);
        }
        Long userId = AuthoInfoCenter.getInstance(session).getUserId();
        return userService.save(userDTO, userId);
    }

    @GetMapping("/search/{userId}")
    @ApiOperation(value = "查询用户详细信息")
    @MenuAutho(desc = "查询用户详细信息")
    @ApiParam(name = "userId", value = "用户ID", required = true)
    public ResultCommon<SysBaseUserDTO> search(@PathVariable Long userId) throws Exception{
        return userService.search(userId);
    }

    @GetMapping("/search/agency/{agencyCode}")
    @ApiOperation(value = "查询机构下的用户")
    @MenuAutho(desc = "查询机构下的用户")
    @ApiParam(name = "agencyCode", value = "机构编号", required = true)
    public ResultCommon<List<SysBaseUserDTO>> searchOfAgency(@PathVariable String agencyCode) throws Exception{
        return userService.searchOfAgency(agencyCode);
    }

    @GetMapping("/search")
    @ApiOperation(value = "查询用户详细信息", notes = "token查询")
    @MenuAutho(desc = "查询用户详细信息")
    public ResultCommon<SysBaseUserDTO> search(HttpSession session) throws Exception{
        Long userId = AuthoInfoCenter.getInstance(session).getUserId();
        return userService.search(userId);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询用户信息")
    @MenuAutho(desc = "分页查询用户信息")
    public ResultCommon<PageData<SysBaseUserDTO>> page(int page, int limit,
                                                       @RequestParam(required = false) Boolean cancel,
                                                       @RequestParam(required = false) Boolean del,
                                                       @RequestParam(required = true) String agencyCode,
                                                       @RequestParam(required = false) String userName) throws Exception{
        return userService.page(page, limit, cancel, del, agencyCode, userName);
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新用户信息", notes = "更新姓名、电话、备注、邮箱")
    @MenuAutho(desc = "更新用户信息")
    @ApiParam(name = "userDTO", value = "用户信息", required = true)
    public ResultCommon<SysBaseUserDTO> update(@RequestBody @Validated(value = {Update.class}) SysBaseUserDTO userDTO,
                                               BindingResult bindingResult,
                                               HttpSession session) throws Exception{
        String error = null;
        if((error = BindingError.error(bindingResult)) != null){
            return ResultCommon.fail(error);
        }
        Long userId = AuthoInfoCenter.getInstance(session).getUserId();
        return userService.update(userDTO, userId);
    }

    @DeleteMapping("/deletes")
    @ApiOperation(value = "删除用户信息")
    @MenuAutho(desc = "删除用户信息")
    @ApiParam(name = "userIds", value = "用户ID数组", required = true)
    public ResultCommon deletes(@RequestBody List<Long> userIds,
                                HttpSession session) throws Exception{
        if(userIds==null || userIds.isEmpty()){
            return ResultCommon.fail("用户列表为空");
        }
        Long userId = AuthoInfoCenter.getInstance(session).getUserId();
        return userService.deletes(userIds, userId);
    }


    @PutMapping("/cancel/{userId}/{status}")
    @ApiOperation(value = "注销用户信息")
    @MenuAutho(desc = "注销用户信息")
    public ResultCommon cancel(@PathVariable Long userId, @PathVariable boolean status,
                               HttpSession session) throws Exception{
        Long auserId = AuthoInfoCenter.getInstance(session).getUserId();
        return userService.cancel(userId, status, auserId);
    }

    /**************************************************/

    /**
     * 为用户分配部门
     * @return
     * @throws Exception
     */
    @PutMapping("/assign/group/{userId}")
    @ApiOperation(value = "为用户分配部门")
    @MenuAutho(desc = "为用户分配部门")
    public ResultCommon<SysBaseUserDTO> assignedGroup(@PathVariable Long userId,
                                                      @RequestBody @Validated(value = {Assign.class})List<SysBaseGroupDTO> groupDTOS,
                                                      BindingResult bindingResult) throws Exception{
        String error = null;
        if((error = BindingError.error(bindingResult)) != null){
            return ResultCommon.fail(error);
        }
        return userService.assignedGroup(userId, groupDTOS);
    }

    @PutMapping("/assign/role/{groupId}/{userId}")
    @ApiOperation(value = "为用户分配部门角色")
    @MenuAutho(desc = "为用户分配部门角色")
    public ResultCommon<SysBaseUserDTO> assignedRoles(@PathVariable Long userId,
                                                      @PathVariable Long groupId,
                                                      @RequestBody @Validated(value = {Assign.class})List<SysBaseRoleDTO> roleDTOS,
                                                      BindingResult bindingResult) throws Exception{
        String error = null;
        if((error = BindingError.error(bindingResult)) != null){
            return ResultCommon.fail(error);
        }
        return userService.assignedRoles(userId, groupId, roleDTOS);
    }

}
